package com.databaseprotect;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class Register extends MainActivity{
    Context context;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        context=this;
        super.onCreate(savedInstanceState);
        setContentView(R.layout.register);
    }
    public void register(View v){
        new Thread(new Runnable() {
            @Override
            public void run() {
                EditText editText_username = (EditText) findViewById(R.id.r_phone_num);
                EditText editText_password = (EditText) findViewById(R.id.r_psw);
                String r_username = editText_username.getText().toString();
                String r_password = editText_password.getText().toString();
                Connection conn = null;
                conn = (Connection) DBHelper.getConn();
                Statement statement = null;
                if (r_password != null && r_username != null) {
                    try {
                        statement = conn.createStatement();
                        String sql = "INSERT INTO user(username,password,type) VALUES('" + r_username + "','" + r_password + "','user');";
                        statement.execute(sql);
                        conn.close();
                        statement.close();
                        Toast.makeText(Register.this,"注册成功", Toast.LENGTH_SHORT).show();
                        finish();
                        Intent intent=new Intent(context,Login.class);
                        startActivity(intent);
                    } catch (SQLException e) {
                        e.printStackTrace();
                    }
                }else {
                    Toast.makeText(Register.this,"请输入手机号和密码", Toast.LENGTH_SHORT).show();
                }
            }
        }).start();
    }
}
